<?php
/*Problem Domain
To perform the actual CRUD operations on Profile by implementing the ProfileDAO interface
*/

include_once 'ProfileDAO.php';
include_once '../model/Profile.php';
include_once '../model/ListUserType.php';

class ProfileDAOImpl implements ProfileDAO
{
	private $conn;
	private $array_list;

	/*void 
	purpose. initializes the newly created UserDAOImpl.*/
	public function __construct()
	{
		$this->conn = new mysqli('localhost' , 'root' , 'root123' , 'tms') or
		die('Connect error : '.$this->conn->connect_error);

		$this->array_list = new ListUserType();
	}

	/*void -> Connection
	purpose. produces the connection object*/
	public function getConnection()
	{
		return $this->conn;
	}

	/*Profile -> boolean
	purpose. produces true if the given Profile is found , false otherwise*/
	private function found($profile)
	{
		$conn = $this->getConnection();
		$user_id = $profile->getId();
		$res = $conn->query("SELECT * FROM Profile where user_id = ".$user_id);
		$status;
		
		if($res->num_rows == 0)
		{
			$status = false;
		}
		else
		{
			$status = true;
		}

		return $status;
	}

	/*Profile -> boolean
	purpose. To persist the given Profile into the database and return true on success
	false otherwise*/
	private function save($profile)
	{
		$conn 	    = $this->getConnection();
		$user_id    = $profile->getId();
		$name 	    = explode('.',$profile->getName());
		$first_name = $name[0];
	   	$last_name  = $name[1];
		$address    = $profile->getAddress();
		$dob	    = $profile->getDateOfBirth();
		$doj	    = $profile->getDateOfJoining();
		$college    = $profile->getCollege();
		
		$stmt = $conn->prepare('INSERT INTO `Profile` VALUES(?, ?, ?, ?, ?, ?, ?)');
		$status = true;

		if(!$stmt)
		{
			echo "\n Prepared Statement error no : ".$stmt->errno."error : ".$stmt->error;
			$status = false;
		}
		else
		{
			if(!$stmt->bind_param('issssss', $user_id, $first_name, $last_name, $address, $dob, $doj, $college))
			{
				echo "Bind error no : ".$stmt->errno."error : ".$stmt->error;
				$status = false;
			}
			else
			{
				if(!$stmt->execute())
				{
					echo "\n Query Execution Failed errno : ".$stmt->errno."error : ".$stmt->error;
					$status = false;
				}
			}
		}

		return $status;		
	}

	/*Profile -> boolean
	pupose. To update the already existing given Profile and return true on success , false otherwise.*/
	public function update($profile)
	{
		$conn       = $this->getConnection();
		$user_id    = $profile->getId();
		$name 	    = explode('.',$profile->getName());
		$first_name = $name[0];
		$last_name  = $name[1];
		$address    = $profile->getAddress();	
		$dob	    = $profile->getDateOfBirth();
		$doj	    = $profile->getDateOfJoining();
		$college    = $profile->getCollege();
		$status     = true;
		$stmt       = $conn->prepare('UPDATE `Profile` set `first_name` = ?, `last_name` = ?, `address` = ?, `dob` = ?, `doj` = ?, `college` = ? WHERE `user_id` = ?');

		if(!$stmt)
		{
			echo "Prepare statement error no : ".$stmt->errno."error : ".$stmt->error;
			$status = false;
		}
		else
		{
			if(!$stmt->bind_param('ssssssi', $first_name, $last_name, $address, $dob, $doj, $college, $user_id))
			{
				echo "Parameter bind error no : ".$stmt->errno."error : ".$stmt->error;
				$status = false;
			}
			else
			{
				if(!$stmt->execute())
				{
					echo "\n Query Execution Failed errno : ".$stmt->errno."error : ".$stmt->error;
					$status = false;
				}
			}
		}

		return $status;
	}

	/*User -> boolean
        purpose. to save or update a given User Profile into the database and return true on success , 
        false otherwise*/
        public function saveOrUpdate($profile)
	{
		$status;

		if(!$this->found($profile))
		{
			$status = $this->save($profile);			
		}
		else
		{
			$status = $this->update($profile);
		}

		return $status;
	}	

	/*Profile -> boolean
        purpose. removes a given User Profile from the database and returns true
        on succes , false otherwise*/
        public function remove($profile)
	{
		$conn = $this->getConnection();
		$status = true;

		if(!$this->found($profile))
		{
			echo "\n".$User->getFirstName()."is not found";
			$status = false;
		}
		else
		{
			$user_id = $profile->getId();
			$sql     = "DELETE FROM Profile where `user_id` = ".$user_id;

			if(!$conn->query($sql))
			{
				echo "Query Execution error no : ".$conn->errno."error : ".$conn->error;
				$status = false;
			}
		}

		return $status;
	}
	
	/*void -> LostUserType
	purpose. produces the entire list of Users Profiles of the system*/
	public function listProfiles()
	{
		$conn = $this->getConnection();
		$sql = "SELECT Profile.user_id, User.emp_id, concat(Profile.first_name, \".\",Profile.last_name) as name, Profile.address, DATE_FORMAT(dob,'%d-%m-%Y') as dob, DATE_FORMAT(doj,'%d-%m-%Y') as doj, Profile.college FROM User , Profile WHERE Profile.user_id = User.user_id AND User.role = '"."trainee"."'";
		$res = $conn->query($sql);

		while($row = $res->fetch_assoc())
		{
			$user_id 	= $row['user_id'];
			$emp_id 	= $row['emp_id'];
			$name		= $row['name'];
			$address	= $row['address'];
			$dob		= $row['dob'];
			$doj		= $row['doj'];
			$college	= $row['college'];
						
			$profile	= new Profile($user_id, $emp_id, $name, $address, $dob, $doj, $college);

			$this->array_list->addToList($profile);
		}

		return $this->array_list;
	}
}
 
?>
